#!/bin/bash

echo "trycompile 0.1"
echo ""
echo "This script tries to compile Haggle with different configurations to see"
echo "to it that Haggle does compile. Each test will take a while. Please be"
echo "patient."
echo ""
echo "WARNING: THIS TEST WILL DELETE MANY FILES IN THE HAGGLE DIRECTORY, AND"
echo "YOU WILL NEED TO RECONFIGURE HAGGLE AFTER RUNNING THIS SCRIPT."
echo ""
echo "DO YOU STILL WISH TO PROCEED? [y/n]"

func_askpermission()
{
	read -s -n 1 answer
	if [ $answer = "y" -o $answer = "yes" ] ; then
		echo "Starting test...";
	elif [ $answer = "n" -o $answer = "no" ] ; then
		echo "Exiting..."
		exit 0
	else
		echo "Please answer \"y\" for yes or \"n\" for no"
		func_askpermission
	fi
}

func_askpermission

# This code does not pipe unwanted outut to /dev/null because that didn't seem 
# to work. Piping it to a grep that finds all empty lines, and then to one that
# finds the nonempty lines does seem to work.

# Remove all files generated by making:
func_realclean()
{
	# This executes the actual code inside the "realclean" make target, but 
	# doesn't call "make realclean", since this function may be called when 
	# there is no makefile.
	svn status --no-ignore | grep "I  " | awk '{print $2}' | xargs rm -rf 2>&1 | grep "^$" | grep -v "^$"
}

# Try to compile with a given set of parameters:
func_trycompile()
{
	echo -n "Attempting to compile with parameters \""$@"\""
	echo -n "."
	autoreconf --install 2>&1 | grep "^$" | grep -v "^$"
	if [ -e configure ] ; then
		echo -n "."
		./configure $@ 2>&1 | grep "^$" | grep -v "^$"
		if [ -e Makefile ] ; then
			echo -n "."
			make 2>&1 | grep "^$" | grep -v "^$"
			if [ -e ./bin/haggle ] ; then
				echo " Passed!"
			else
				echo " FAILED! (make)"
			fi
		else
			echo " FAILED! (configure)"
		fi
	else
		echo " FAILED! (autoreconf)"
	fi
	func_realclean
}

# Clean up first, so we don't get a wrong result because of old files:
func_realclean

# Perform tests:
func_trycompile 
func_trycompile --enable-debug
func_trycompile --enable-debug-leaks
func_trycompile --enable-debug --enable-debug-leaks
func_trycompile --disable-ethernet
func_trycompile --disable-bluetooth
func_trycompile --disable-media